package com.oazon.moco.lan.core.http;


import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.nio.charset.Charset;




import android.util.Log;

import com.alibaba.fastjson.JSON;

public abstract class AbsHttpJsonDecoder<T> extends MessageToMessageDecoder<T>{

	private final static String CHARSET_NAME = "UTF-8";
	private static Charset UTF_8 = Charset.forName(CHARSET_NAME);
	private Class<?> clazz; 
	
	protected AbsHttpJsonDecoder(Class<?> clazz) {
		this.clazz = clazz;
	}

	protected Object decode(ChannelHandlerContext cxt, ByteBuf body) throws Exception{
		String content = body.toString(UTF_8);
		Object result =  null;
		try {
			result = JSON.parseObject(content, clazz);
			Log.d("Decode", content);
		} catch (Exception e) {
			Log.e("Decode", e.getMessage());
			throw e;
		}
		
		return result;
	}
}
